package com.mingtu.jgspatrol.adapter;

import android.content.Context;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.luck.picture.lib.entity.LocalMedia;
import com.mingtu.jgspatrol.R;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;


public class LotAdapter extends BaseAdapter implements View.OnClickListener {
    private Context context;
    private OnItemClickListener mOnItemClickListener = null;
    private List<LocalMedia> mData = new ArrayList<>();
    private LinkedHashMap<Integer, Object> hashMap = new LinkedHashMap<>();

    public LotAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @RequiresApi(api = Build.VERSION_CODES.P)
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.item_lot, null);

            holder.layout = (FrameLayout) convertView.findViewById(R.id.layout);
            holder.ivImg = (ImageView) convertView.findViewById(R.id.iv_img);
            holder.tvText1 = (TextView) convertView.findViewById(R.id.tv_text1);
            holder.tvText2 = (TextView) convertView.findViewById(R.id.tv_text2);

            ViewGroup.LayoutParams lp = holder.layout.getLayoutParams();
            int screenWidth = ScreenUtils.getScreenWidth(); //照片一行3个，间隙是20dp
            int mWidth = (screenWidth - SizeUtils.dp2px(15) * 2 - SizeUtils.dp2px(6) * 1) / 2;
            int mHeight = mWidth * SizeUtils.dp2px(120) / SizeUtils.dp2px(175);
            lp.width = mWidth;
            lp.height = mHeight;
            holder.layout.setLayoutParams(lp);

            holder.layout.setOnClickListener(this);
            holder.layout.setTag(position);
            convertView.setTag(holder);
        } else {

            holder = (ViewHolder) convertView.getTag();
        }


        switch (position) {

            case 0:
                holder.ivImg.setBackgroundResource(R.mipmap.img_lot_module2);
                holder.tvText1.setText("红外监测");
                holder.tvText2.setText("Infrared monitoring");
                break;
            case 1:
                holder.ivImg.setBackgroundResource(R.mipmap.img_lot_module4);
                holder.tvText1.setText("视频监控");
                holder.tvText2.setText("Monitoring");
                break;

            case 2:
                holder.ivImg.setBackgroundResource(R.mipmap.img_lot_module1);
                holder.tvText1.setText("生态监测");
                holder.tvText2.setText("Ecological monitoring");
                break;

//            case 3:
//                holder.ivImg.setBackgroundResource(R.mipmap.img_lot_module3);
//                holder.tvText1.setText("无人机");
//                holder.tvText2.setText("Drone");
//                break;
        }

        return convertView;
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用tag方法获取position
            try {
                mOnItemClickListener.onItemClick(v, (int) v.getTag());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    public List<LocalMedia> getData() {
        return mData;
    }


    class ViewHolder {
        public FrameLayout layout;
        public ImageView ivImg;
        public TextView tvText1;
        public TextView tvText2;
    }


    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

}
